/*
 * WidgetButton.cpp
 *
 *	Author: stravant
 *	Description:
 *		
 */

#include "WidgetButton.h"
#include "../ui/Canvas.h"
#include "WidgetRoot.h"

namespace tbt {

WidgetButton::WidgetButton(): mImage(0), mHover(false), mPressed(false),
		mHoverAnimation(TimeDelta::FromMilliseconds(200), this) {

}

WidgetButton::~WidgetButton() {}

void WidgetButton::onPaint(const IRect& region, fx::Context* c) {
	DRect r = getBounds().to<double>();
	c->save();

	//pattern
	fx::Pattern* pat = fx::Pattern::CreateLinear(r.x(), r.top(), r.x(), r.bottom());
//	if (mPressed) {
//		pat->addColorStop(0.0, getBackgroundColor().darker(0.10));
//		pat->addColorStop(0.7, getBackgroundColor().darker(0.05));
//		pat->addColorStop(1.0, getBackgroundColor().lighter(0.0));
//	} else if (mHover) {
//		pat->addColorStop(0.0, getBackgroundColor().lighter(0.20));
//		pat->addColorStop(1.0, getBackgroundColor().darker(0.05));
//	} else {
//		pat->addColorStop(0.0, getBackgroundColor().lighter(0.15));
//		pat->addColorStop(1.0, getBackgroundColor().darker(0.10));
//	}
	if (mPressed) {
		pat->addColorStop(0.0, getBackgroundColor().darker(0.10));
		pat->addColorStop(0.7, getBackgroundColor().darker(0.05));
		pat->addColorStop(1.0, getBackgroundColor().lighter(0.0));
	} else {
		pat->addColorStop(0.0, getBackgroundColor().lighter(
			mHoverAnimation.getValueBetween(0.10, 0.30)));
		pat->addColorStop(1.0, getBackgroundColor().darker(
			mHoverAnimation.getValueBetween(0.20, 0.05)));
	}

	fx::Pattern* pat2 = fx::Pattern::CreateLinear(r.x(), r.top(), r.x(), r.bottom());
	if (mPressed) {
		fx::Color inv = getBackgroundColor().inverse();
		pat2->addColorStop(0.0, inv.lighter(0.10));
		pat2->addColorStop(1.0, inv.lighter(0.10));
	} else {
		fx::Color inv = getBackgroundColor().inverse();
		pat2->addColorStop(0.0, getBackgroundColor().darker(0.30));
		pat2->addColorStop(1.0, inv.lighter(0.10));
	}

	//clip to update region
	c->rectangle(region.to<double>());
	c->clip();
	c->roundedRectangle(r, 3.0);
	c->clip();

	//draw button area
	c->roundedRectangle(r, 3.0);
	c->setSource(pat);
	c->fillPreserve();

	//c->setSourceColor(getBackgroundColor());
	c->setSource(pat2);
	c->setLineWidth(1.5);
	c->stroke();

	//text
	if (!mText.empty()) {
		const char* text = mText.c_str();
		cairo_t* cr = c->getInternal();
		//
		cairo_text_extents_t ext;
		cairo_set_font_size(cr, getFontSize());
		cairo_text_extents(cr, text, &ext);
		//
		c->setSourceColor(getTextColor());
		c->moveTo(r.position() + r.size()/2.0);
		c->relMoveTo(-ext.width/2.0, ext.height/2.0);
		if (mPressed) {
			c->relMoveTo(1, 1);
		}
		cairo_show_text(cr, text);
	}

	//restore
	c->restore();

	//clean up
	delete pat;
	delete pat2;
}

void WidgetButton::onEvent_Crossing(const MouseEvent& e) {
	if (e.getType() == Event::kEvtMouseEnter) {
		mHover = true;
		mHoverAnimation.slideShow();
		//getRoot()->performPaint(getBounds(), getRoot()->getContext());
	} else {
		mHover = false;
		mPressed = false;
		mHoverAnimation.slideHide();
		//getRoot()->performPaint(getBounds(), getRoot()->getContext());
	}
}

void WidgetButton::onEvent_Button(const MouseButtonEvent& e) {
	if (e.getButton() == Mouse::kButtonLeft) {
		if (e.getType() == Event::kEvtMouseDown) {
			mPressed = true;
			getRoot()->performPaint(getBounds(), getRoot()->getContext());
		} else {
			mPressed = false;
			getRoot()->performPaint(getBounds(), getRoot()->getContext());
		}
	}
}

void WidgetButton::animationProgressed(Animation* anim) {
	//invoke a repaint of the button
	getRoot()->performPaint(getBounds(), getRoot()->getContext());
}

}




















